
# REQUIRES: Data/Framing the Exit Experiment Raw Data.csv

################# Dependencies #################
# source("Load Packages.R")
# source("Analysis - Experiment/Cleaning.R")

################# Figure S2 - Interactions of Enemy Victory Frame with Foreign Policy Predispositions #################
### H3b
### Military Force
data$military_force[is.na(data$military_force)] = 2
data$t_mf <- data$taliban*data$military_force

h3bi <- glm(withdraw_yn ~ taliban*military_force, data=data, family="binomial")
summary(h3bi)
h3bi %>% coef() %>% exp()

# Evaluated at means
allmean <- data.frame(taliban=mean(data$taliban),
                      military_force=mean(data$military_force),
                      t_mf=mean(data$t_mf))
allmean$pred.prob <- predict(h3bi, newdata=allmean, type="response")
allmean <- cbind(allmean,predict(h3bi, newdata=allmean, type="response", se.fit=TRUE))
allmean

# Evaluated at means of military_force and t_mf by taliban condition
h3a2 <- glm(withdraw_yn ~ taliban_recode+military_force+t_mf, data=data, family="binomial")
summary(h3a2)
allmean2 <- data.frame(military_force=rep(mean(data$military_force),2),
                       t_mf=rep(mean(data$t_mf),2),
                       taliban_recode=as.factor(c('control', 'taliban')))
allmean2 <- cbind(allmean2,predict(h3a2, newdata=allmean2, type="response", se.fit=TRUE))
allmean2

logitmfx(withdraw_yn ~ taliban*military_force, data=data)

# Interplot
margin <- unit(0.5, "line")
grob1 <- interplot(m = h3bi, var1 = "taliban", var2 = "military_force") +
  theme_light() +
  theme(text=element_text(family="Times New Roman"), axis.text.x=element_blank()) +
  ylim(-1.85, 0) +
  #scale_x_discrete(labels = c("Strongly Disagree", "Disagree", "Neither Agree nor Disagree", "Agree", "Strongly Agree")) +
  labs(y = "Marginal Effect on Support for Withdrawal") +
  geom_hline(yintercept = 0, linetype = "dashed")
grob2 <- textGrob("   ", gp=gpar(fontsize=11, fontfamily="Times New Roman"))
grob3 <- textGrob("Support for the Use of Force", gp=gpar(fontsize=11, fontfamily="Times New Roman"))

plot4a <- grid.arrange(grob1, grob2, grob3, 
                       heights = unit.c(unit(1,"null"), grobHeight(grob2) + 1.2*margin, grobHeight(grob3) + 1.8*margin),
                       vp=viewport(width=0.95, height=0.95))
# ggsave("Plots/Figure 4a_nolabels.png", plot4a)


### Great Powers
data$t_gp <- data$taliban*data$great_powers

h3bii <- glm(withdraw_yn ~ taliban*great_powers, data=data, family="binomial")
summary(h3bii)
h3bii %>% coef() %>% exp()

# Evaluated at means
allmean <- data.frame(taliban=mean(data$taliban),
                      great_powers=mean(data$great_powers),
                      t_gp=mean(data$t_gp))
allmean$pred.prob <- predict(h3bii, newdata=allmean, type="response")
allmean <- cbind(allmean,predict(h3bii, newdata=allmean, type="response", se.fit=TRUE))
allmean

# Evaluated at means of great_powers and t_gp by taliban condition
h3a2 <- glm(withdraw_yn ~ taliban_recode+great_powers+t_gp, data=data, family="binomial")
summary(h3a2)
allmean2 <- data.frame(great_powers=rep(mean(data$great_powers),2),
                       t_gp=rep(mean(data$t_gp),2),
                       taliban_recode=as.factor(c('control', 'taliban')))
allmean2 <- cbind(allmean2,predict(h3a2, newdata=allmean2, type="response", se.fit=TRUE))
allmean2

logitmfx(withdraw_yn ~ taliban*great_powers, data=data)

# Interplot
margin <- unit(0.5, "line")
grob1 <- interplot(m = h3bii, var1 = "taliban", var2 = "great_powers") +
  theme_light() +
  theme(text=element_text(family="Times New Roman"), axis.text.x=element_blank()) +
  ylim(-1.85, 0) +
  #scale_x_discrete(labels = c("Strongly Disagree", "Disagree", "Neither Agree nor Disagree", "Agree", "Strongly Agree")) +
  labs(y = "Marginal Effect on Support for Withdrawal") +
  geom_hline(yintercept = 0, linetype = "dashed")
grob2 <- textGrob("   ", gp=gpar(fontsize=11, fontfamily="Times New Roman"))
grob3 <- textGrob("Support for Great Power Policing", gp=gpar(fontsize=11, fontfamily="Times New Roman"))

plot4b <- grid.arrange(grob1, grob2, grob3, 
                       heights = unit.c(unit(1,"null"), grobHeight(grob2) + 1.2*margin, grobHeight(grob3) + 1.8*margin),
                       vp=viewport(width=0.95, height=0.95))
# ggsave("Plots/Figure 4b_nolabels.png", plot4b)


### Figure 4
grob4 <- grid.arrange(plot4a, plot4b,
                      heights = unit.c(unit(1,"null")),
                      vp=viewport(width=0.95, height=0.95))
grob5 <- textGrob("Figure S2: Interactions of Enemy Victory Frame with Foreign Policy Predispositions", gp=gpar(fontsize=13, fontfamily="Times New Roman"))

plot4 <- grid.arrange(grob4, grob5,
                      heights = unit.c(unit(1,"null"), grobHeight(grob3) + 1.2*margin),
                      vp=viewport(width=0.95, height=0.95))

ggsave("Plots/Figure S2 - Interactions of Enemy Victory Frame with Foreign Policy Predispositions.png", plot4, width=12, height=6)

rm(allmean, allmean2, grob1, grob2, grob3, grob4, grob5, h3a2, h3bi, h3bii, plot4, plot4a, plot4b, margin)
